<?php

namespace App\Winged;

use App\Winged\Middleware\Authenticate;
use App\Winged\Middleware\EnableCrossRequest;
use App\Winged\Middleware\OperationLog;
use App\Winged\Middleware\PermissionMiddleware;
use App\Winged\Middleware\Tenant;
use RuntimeException;
use Illuminate\Support\Facades\Route;
use Encore\Admin\Admin;

/*
 * 所有模块ServiceProvider的基类
 */

abstract class WingedModuleServiceProvider extends \Illuminate\Support\ServiceProvider
{
    protected $prefix = "winged.";
    /**
     * Name of the module
     */
    protected $moduleName = null;


    protected $serviceName = null ;

    /**
     * @var null
     */
    protected $modulePrefix = null;

    protected $middleware = [
        'tenant'             => Tenant::class, // 租户服务
        'auth'               => Authenticate::class, // 用户认证
        'permission'         => PermissionMiddleware::class, // 权限系统
        'enableCrossRequest' => EnableCrossRequest::class, // 跨域控制
        'log'                => OperationLog::class, //操作日志
    ];

    protected $middlewareGroups = [
        'tenants' => [
            'tenant',
        ],
        'admin' => [
            'tenant',
            'auth',
            'log',
            'permission',
            'enableCrossRequest',
        ],
    ];

    public function __construct($app, $module)
    {
        parent::__construct($app);
        $this->setModule($module);
    }

    /**
     * 模块名称设置
     *
     * @param $moduleName
     */
    protected function setModule($moduleName)
    {
        $this->moduleName   = $moduleName;
        $this->modulePrefix = kebab_case($this->moduleName);
    }


    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerRouteMiddleware();
        $this->onRegister();
    }


    /**
     * Register the route middleware.
     *
     * @return void
     */
    private function registerRouteMiddleware()
    {
        foreach ($this->middleware as $key => $middleware) {
            app('router')->aliasMiddleware($key, $middleware);
        }

        // register middleware group.
        foreach ($this->middlewareGroups as $key => $middleware) {
            app('router')->middlewareGroup($key, $middleware);
        }
    }

    /**
     * Child class has to override this method.
     */
    abstract protected function onRegister() ;

    /**
     * Boot the service provider.
     *
     * @return void
     */
    public function boot()
    {
        if (empty($this->moduleName)) {
            throw new RuntimeException('[WingedModuleServiceProvider] moduleName is not set, call setModule in child class constructor!');
        }
        $this->onBoot();
    }

    /**
     * Child class has to override this method.
     */
    abstract protected function onBoot();

    /**
     * @param $modulePath
     */
    protected function registerViews($modulePath)
    {
        $this->loadViewsFrom($modulePath . '/Views', 'winged.' . $this->modulePrefix);
    }

    public function getServiceName()
    {
        return $this->serviceName ;
    }

}
